package ru.aslanov.schedule.model;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

/**
 * Created by IntelliJ IDEA.
 * Created: Mar 10, 2010 1:04:25 PM
 *
 * @author Sergey Aslanov
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class GoogleCalendarSyncEvent extends Entity {
    @Persistent
    private Operation operation;
    @Persistent
    private String googleCalendarId;
    @Persistent
    private String googleEventId;

    @NotPersistent
    private UnownReference<ScheduleItem> scheduleItem = new UnownReference<ScheduleItem>(ScheduleItem.class);
    @NotPersistent
    private UnownReference<Group> group = new UnownReference<Group>(Group.class);    
    @NotPersistent
    private UnownReference<Schedule> schedule = new UnownReference<Schedule>(Schedule.class);

    @Persistent
    private long time;
    @Persistent
    private int num;
    @Persistent
    private int retryCount;


    public GoogleCalendarSyncEvent() {
    }

    private GoogleCalendarSyncEvent(Operation operation, String googleCalendarId, String googleEventId, ScheduleItem scheduleItem, Group group) {
        this.operation = operation;
        this.googleCalendarId = googleCalendarId;
        this.googleEventId = googleEventId;
        if (scheduleItem != null)
            this.scheduleItem.setEncodedKey(scheduleItem.getEncodedKey());
        if (group != null)
            this.group.setEncodedKey(group.getEncodedKey());
    }

    public GoogleCalendarSyncEvent(String googleEventId, String googleCalendarId, ScheduleItem scheduleItem) {
        this(Operation.REMOVE, googleCalendarId, googleEventId, scheduleItem, null);
    }

    public GoogleCalendarSyncEvent(Group group, String googleCalendarId, Operation operation) {
        this(operation, googleCalendarId, null, null, group);
    }

    public GoogleCalendarSyncEvent(ScheduleItem scheduleItem, String googleCalendarId, Operation operation) {
        this(operation, googleCalendarId, null, scheduleItem, null);
    }


    public Operation getOperation() {
        return operation;
    }

    public String getGoogleEventId() {
        return googleEventId;
    }

    public UnownReference<ScheduleItem> getScheduleItem() {
        return scheduleItem;
    }

    public String getGoogleCalendarId() {
        return googleCalendarId;
    }

    public UnownReference<Group> getGroup() {
        return group;
    }

    @Persistent
    public String getScheduleItemKey() {
        return scheduleItem.getEncodedKey();
    }

    public void setScheduleItemKey(String scheduleItemKey) {
        this.scheduleItem.setEncodedKey(scheduleItemKey);
    }

    @Persistent
    public String getGroupKey() {
        return group.getEncodedKey();
    }

    public void setGroupKey(String groupKey) {
        this.group.setEncodedKey(groupKey);
    }

    @Persistent
    public String getScheduleKey() {
        return this.schedule.getEncodedKey();
    }

    public void setScheduleKey(String scheduleKey) {
        this.schedule.setEncodedKey(scheduleKey);
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getRetryCount() {
        return retryCount;
    }

    public void setRetryCount(int retryCount) {
        this.retryCount = retryCount;
    }

    @Override
    public String toString() {
        if (operation == Operation.REMOVE) {
            return operation + ": calId=" + googleCalendarId + ", eventId=" + googleEventId;
        } else {
            return operation + ": "
                    + (group.getEncodedKey() != null ? "group=" + group.getEncodedKey() : "")
                    + (scheduleItem.getEncodedKey() != null ? "scheduleItem=" + scheduleItem.getEncodedKey() : "");
        }
    }
}
